Ext.onReady(function(){ 

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var sm = new Ext.grid.CheckboxSelectionModel();

var cm=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40}),
{header:'流水号',dataIndex:'serialNo',hidden:true},
{header:'被保证人',dataIndex:'loanerName',width:80},
{header:'保证类型',dataIndex:'guaranteeType',width:80},
{header:'笔数',dataIndex:'guaranteeCount',width:60,hidden:true},
{header:'担保金额',dataIndex:'balance',width:60},
{header:'代偿可能性',dataIndex:'compensatoryPossibility',width:60},
{header:'抵（质）押权人名称',dataIndex:'guarantorName',width:80},
{header:'抵（质）押标的物名称',dataIndex:'guarantyName',width:80},
{header:'抵（质）押标的物价值',dataIndex:'guarantySum',width:80},
{header:'抵（质）押期限',dataIndex:'guarantyTerm',width:80},
{header:'抵（质）押债务余额',dataIndex:'guarantyBalance',width:80},
{header:'抵债可能性',dataIndex:'guarantyPossibility',width:80,hidden:true}
]); 

var ds=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'customer/listGuarantee.do',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'serialNo'},
{name:'loanerName'}, 
{name:'guaranteeType'}, 
{name:'guaranteeCount'},
{name:'balance'},
{name:'compensatoryPossibility'},
{name:'guarantorName'},
{name:'guarantyName'},
{name:'guarantySum'},
{name:'guarantyTerm'},
{name:'guarantyBalance'},
{name:'guarantyPossibility'}
]) 
}); 
ds.load({params:{start:0,limit:10}}); 

var guarantyTypeStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=guaranteeType',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) ,
	autoLoad: true
}); 

var grid=new Ext.grid.GridPanel({ 
id:'contentList',
renderTo:"list", 
stripeRows:true,//斑马线效果
title:"银企合作-对外担保信息列表", 
height:500, 
cm:cm,
store: ds,
loadMask: { msg: "加载中..." }, 
viewConfig: {   
forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 10,   
                store: ds, 
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            }),
 tbar: [
 {text:'新增',id: 'btnAdd'},
 '-',
 {text:'查看/编辑',handler:check},
 '-',
 {text:'删除',handler:del}
 ]              
});

//新增表单
addForm = new Ext.form.FormPanel({ 
id: 'addForm', 
width: 700, 
height: 320, 
//样式 
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 160, 
items: 
[ { xtype: 'fieldset', 
title: '对外担保信息', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[ 
{
	layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
        	   xtype: 'combo',
        		 id : "guaranteeType",
        		 name: 'guaranteeType',
        	     store :guarantyTypeStore,
        	     fieldLabel: '担保类型',
        	     valueField : 'code',
        	     displayField : 'name',
        	     emptyText : '请选择担保类型...',
        	     allowBlank: true,
        	     blankText : '请选择担保类型',
        	     editable: false,
        	     typeAhead : true,
        	     width: 130,
        	     mode:'remote',  
        	     triggerAction : 'all',
        	     selectOnFocus : true,
        	     hiddenName:'guaranteeType',
        	     
        	     listeners:{        
	                  select : function(combo, record,index)   
	                  {   
	                	 // alert(combo.value);
	                var	compensatoryPossibility = Ext.getCmp('compensatoryPossibility');
	                var	balance = Ext.getCmp('balance');
        	     	var	guarantorName = Ext.getCmp('guarantorName');
        	     	var	guarantyName = Ext.getCmp('guarantyName');
        	     	var	guarantySum = Ext.getCmp('guarantySum');
        	     	var	guarantyTerm = Ext.getCmp('guarantyTerm');
        	     	var	guarantyBalance = Ext.getCmp('guarantyBalance');
        	     	var	guarantyPossibility = Ext.getCmp('guarantyPossibility');
        	     		
	                	  
	                if(combo.value=='01'){
	                	compensatoryPossibility.show();
	                	balance.show();
	                	guarantorName.hide();
	                	guarantyName.hide();
	                	guarantySum.hide();
	                	guarantyTerm.hide();
	                	guarantyBalance.hide();
	                	guarantyPossibility.hide();
	                }else{
	                	compensatoryPossibility.hide();
	                	balance.hide();
	                	guarantorName.show();
	                	guarantyName.show();
	                	guarantySum.show();
	                	guarantyTerm.show();
	                	guarantyBalance.show();
	                	guarantyPossibility.show();
	                }
	                
	              }
	            }
        	     
		}]}
]},
 
  {
	layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'textfield',
id: 'loanerName', 
name: 'loanerName', 
fieldLabel: '被保证人',
emptyText: '必填', 
blankText: '被保证人不能为空', 
allowBlank: false, 
width: 130 
}]},{ columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'numberfield',
id: 'guaranteeCount', 
name: 'guaranteeCount', 
fieldLabel: '笔数',
allowDecimals:false,               //不允许输入小数  
nanText:'请输入有效整数',           //无效数字提示  
allowNegative:false,  
width: 130          
}]} 
]},{
	layout:"column",
items:[{
	columnWidth : .5, 
           layout : "form",
           items : [{
xtype: 'numberfield',
id: 'balance', 
name: 'balance', 
fieldLabel: '担保金额（元）',
allowDecimals:true,               
allowNegative:false,  
width: 130 
}]},{ columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'numberfield',
id: 'compensatoryPossibility', 
name: 'compensatoryPossibility', 
fieldLabel: '代偿可能性（%）',
allowDecimals:true,  
allowNegative:false,  
width: 130          
}]} 
]},{
	layout:"column",
items:[{
	columnWidth : .5, 
           layout : "form",
           items : [{
xtype: 'textfield',
id: 'guarantorName', 
name: 'guarantorName', 
fieldLabel: '抵（质）押权人名称',
width: 130 
}]},{ columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'textfield',
id: 'guarantyName', 
name: 'guarantyName', 
fieldLabel: '抵（质）押标的物名称',
width: 130          
}]} 
]},{
	layout:"column",
items:[{
	columnWidth : .5, 
           layout : "form",
           items : [{
xtype: 'numberfield',
id: 'guarantySum', 
name: 'guarantySum', 
fieldLabel: '抵（质）押标的物价值（元）',
allowDecimals:true,  
allowNegative:false,  
width: 130 
}]},{ columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'numberfield',
id: 'guarantyTerm', 
name: 'guarantyTerm', 
fieldLabel: '抵（质）押期限（月）',
allowDecimals:false,  
allowNegative:false,  
width: 130         
}]} 
]},{
	layout:"column",
items:[{
	columnWidth : .5, 
           layout : "form",
           items : [{
xtype: 'numberfield',
id: 'guarantyBalance', 
name: 'guarantyBalance', 
fieldLabel: '抵（质）押债务余额（元）',
allowDecimals:true,  
allowNegative:false,  
width: 130 
}]},{ columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'numberfield',
id: 'guarantyPossibility', 
name: 'guarantyPossibility', 
fieldLabel: '抵债可能性（%）',
allowDecimals:true,  
allowNegative:false,  
width: 130         
}]} 
]},
{ layout : "form",
           items : [{
xtype: 'hidden', 
 id : "serialNo",
              fieldLabel: '对外担保流水号',
              allowBlank: true
}]}
]}]});

//新增窗口 
addWin = new Ext.Window({ 
id: 'addWin', 
title: '客户-担保信息', 
width: 720, 
height: 300, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addForm, 
buttons: 
[  
{ text: '保存', id: 'btnSubmit' } ,
{ text: '关闭', handler: function() { Ext.getCmp('addWin').hide(); } }

] 
});

function addFunction() {

	
	var addForm = Ext.getCmp("addForm");
if(addForm.form.isValid()){
	 addForm.form.doAction('submit', { 
	 url : 'customer/addGuarantee.do', 
	 method : 'post', 
	 success : function() { 
	 grid.store.reload();
	 Ext.Msg.alert('提示', '保存成功！'); 
	 }, failure : function() { 
	  Ext.Msg.alert('提示', '保存失败！'); 
	  } 
});  
}
 }
  


//添加按钮单击事件 
function btnAddClick() { 
Ext.getCmp("addWin").show();
addForm.getForm().getEl().dom.reset(); 
Ext.getCmp("addWin").hide(); 
	
Ext.getCmp('addForm').form.reset(); 
Ext.getCmp("addWin").buttons[0].handler = addFunction; 
Ext.getCmp("addWin").show(); 
}; 

//新增按钮 
var addUserBtn = Ext.getCmp('btnAdd'); 
addUserBtn.on('click', btnAddClick); 

//删除地址
function del(){
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要删除的内容！"); 
} 
else{
Ext.MessageBox.confirm('提示', '确实要删除所选的内容吗？', deleteConfirm);
}
}

function deleteConfirm(btn){
if (btn == 'yes') {
//alert(grid.getSelectionModel().getSelections()[0].data.serialNo);
 Ext.Ajax.request({
  url:'customer/deleteGuarantee.do',
  params : { serialNo: grid.getSelectionModel().getSelections()[0].data.serialNo},
  method:'POST',
  success:function(){
   Ext.MessageBox.alert("提示","删除成功！");
   grid.store.reload(); 
  },failure:function(){
    Ext.MessageBox.alert("提示", "删除失败！"); 
  }
});
}
}

//编辑地址
function editFunction(){
	
	var addForm = Ext.getCmp("addForm");
if(addForm.form.isValid()){
	 addForm.form.doAction('submit', { 
                    url : 'customer/editGuarantee.do', 
                    method : 'post', 
                       success : function() { 
                       grid.store.reload();
                       Ext.Msg.alert('提示', '保存成功！'); 
                       }, 
                        failure : function() { 

                           Ext.Msg.alert('提示', '保存失败！'); 
 
                        } 
                    }); 
                    }
}

function check(){
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要编辑的内容！"); 
}
else{
	var serialNo = grid.getSelectionModel().getSelections()[0].data.serialNo;
	
	Ext.getCmp('addForm').form.reset(); 
	Ext.getCmp("addWin").buttons[0].handler = editFunction; 

	Ext.Ajax.request({
url:'customer/getGuarantee.do',
params : { serialNo: grid.getSelectionModel().getSelections()[0].data.serialNo},
method:'POST',
  success:function(response){
 // alert(response.responseText);
 var jo = Ext.util.JSON.decode(response.responseText);
  	Ext.getCmp("serialNo").setValue(jo.serialNo);
	Ext.getCmp("loanerName").setValue(jo.loanerName);
	Ext.getCmp("guaranteeType").setValue(jo.guaranteeType);
  	Ext.getCmp("guaranteeCount").setValue(jo.guaranteeCount);
  	Ext.getCmp("balance").setValue(jo.balance);
  	Ext.getCmp("compensatoryPossibility").setValue(jo.compensatoryPossibility);
  	Ext.getCmp("guarantorName").setValue(jo.guarantorName);
  	Ext.getCmp("guarantyName").setValue(jo.guarantyName);
  	Ext.getCmp("guarantySum").setValue(jo.guarantySum);
  	Ext.getCmp("guarantyTerm").setValue(jo.guarantyTerm);
  	Ext.getCmp("guarantyBalance").setValue(jo.guarantyBalance);
  	Ext.getCmp("guarantyPossibility").setValue(jo.guarantyPossibility);
	Ext.getCmp("addWin").show(); 
	
	
	var	compensatoryPossibility = Ext.getCmp('compensatoryPossibility');
    var	balance = Ext.getCmp('balance');
   	var	guarantorName = Ext.getCmp('guarantorName');
   	var	guarantyName = Ext.getCmp('guarantyName');
   	var	guarantySum = Ext.getCmp('guarantySum');
   	var	guarantyTerm = Ext.getCmp('guarantyTerm');
   	var	guarantyBalance = Ext.getCmp('guarantyBalance');
   	var	guarantyPossibility = Ext.getCmp('guarantyPossibility');
   	
	if(jo.guaranteeType=="01"){
		compensatoryPossibility.show();
    	balance.show();
    	guarantorName.hide();
    	guarantyName.hide();
    	guarantySum.hide();
    	guarantyTerm.hide();
    	guarantyBalance.hide();
    	guarantyPossibility.hide();	
	}else{
		compensatoryPossibility.hide();
    	balance.hide();
    	guarantorName.show();
    	guarantyName.show();
    	guarantySum.show();
    	guarantyTerm.show();
    	guarantyBalance.show();
    	guarantyPossibility.show();
	}
	
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
}); 
}
}
grid.addListener('rowdblclick',check);
}); 